﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text.RegularExpressions;

    [ScriptType("SP.GroupCollection", ServerTypeId="{0b9f0e6c-2c15-425e-b0b2-961f78bf1ecf}")]
    public class GroupCollection : ClientObjectCollection<Microsoft.SharePoint.Client.Group>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public GroupCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public Microsoft.SharePoint.Client.Group Add(GroupCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if (parameters.Title == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Title");
                    }
                    if ((parameters.Title != null) && (parameters.Title.Length == 0))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                    if ((parameters.Title != null) && (parameters.Title.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                    if ((parameters.Title != null) && !Regex.Match(parameters.Title, "[^\\s\"/\\\\\\[\\]:|<>+=;,?*'@]+[^\"/\\\\\\[\\]:|<>+=;,?*'@]*").Success)
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                    if ((parameters.Description != null) && (parameters.Description.Length > 0x200))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Description");
                    }
                }
            }
            Microsoft.SharePoint.Client.Group group = new Microsoft.SharePoint.Client.Group(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            group.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(group.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, group);
            base.Context.AddQuery(query);
            base.AddChild(group);
            group.InitFromCreationInformation(parameters);
            return group;
        }

        [Remote]
        public Microsoft.SharePoint.Client.Group GetById(int id)
        {
            Dictionary<int, Microsoft.SharePoint.Client.Group> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<int, Microsoft.SharePoint.Client.Group>) obj2;
            }
            else
            {
                dictionary = new Dictionary<int, Microsoft.SharePoint.Client.Group>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            Microsoft.SharePoint.Client.Group group = null;
            if (!dictionary.TryGetValue(id, out group))
            {
                group = new Microsoft.SharePoint.Client.Group(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { id }));
                dictionary[id] = group;
                ObjectIdentityQuery query = new ObjectIdentityQuery(group.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, group);
                base.Context.AddQuery(query);
            }
            return group;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }

        [Remote]
        public void Remove(Microsoft.SharePoint.Client.Group group)
        {
            if (base.Context.ValidateOnClient && (group == null))
            {
                throw ClientUtility.CreateArgumentNullException("group");
            }
            ClientAction query = new ClientActionInvokeMethod(this, "Remove", new object[] { group });
            base.Context.AddQuery(query);
            base.RemoveChild(group);
        }
    }
}

